{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "google",
   "metadata": {},
   "source": [
    "##### Copyright 2025 Google LLC."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "apache",
   "metadata": {},
   "source": [
    "Licensed under the Apache License, Version 2.0 (the \"License\");\n",
    "you may not use this file except in compliance with the License.\n",
    "You may obtain a copy of the License at\n",
    "\n",
    "    http://www.apache.org/licenses/LICENSE-2.0\n",
    "\n",
    "Unless required by applicable law or agreed to in writing, software\n",
    "distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "See the License for the specific language governing permissions and\n",
    "limitations under the License.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "basename",
   "metadata": {},
   "source": [
    "# killer_sudoku"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "link",
   "metadata": {},
   "source": [
    "<table align=\"left\">\n",
    "<td>\n",
    "<a href=\"https://colab.research.google.com/github/google/or-tools/blob/main/examples/notebook/contrib/killer_sudoku.ipynb\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/colab_32px.png\"/>Run in Google Colab</a>\n",
    "</td>\n",
    "<td>\n",
    "<a href=\"https://github.com/google/or-tools/blob/main/examples/contrib/killer_sudoku.py\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/github_32px.png\"/>View source on GitHub</a>\n",
    "</td>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "doc",
   "metadata": {},
   "source": [
    "First, you must install [ortools](https://pypi.org/project/ortools/) package in this colab."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "install",
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install ortools"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "description",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "  Killer Sudoku in Google CP Solver.\n",
    "\n",
    "  http://en.wikipedia.org/wiki/Killer_Sudoku\n",
    "  '''\n",
    "  Killer sudoku (also killer su doku, sumdoku, sum doku, addoku, or\n",
    "  samunamupure) is a puzzle that combines elements of sudoku and kakuro.\n",
    "  Despite the name, the simpler killer sudokus can be easier to solve\n",
    "  than regular sudokus, depending on the solver's skill at mental arithmetic;\n",
    "  the hardest ones, however, can take hours to crack.\n",
    "\n",
    "  ...\n",
    "\n",
    "  The objective is to fill the grid with numbers from 1 to 9 in a way that\n",
    "  the following conditions are met:\n",
    "\n",
    "    * Each row, column, and nonet contains each number exactly once.\n",
    "    * The sum of all numbers in a cage must match the small number printed\n",
    "      in its corner.\n",
    "    * No number appears more than once in a cage. (This is the standard rule\n",
    "      for killer sudokus, and implies that no cage can include more\n",
    "      than 9 cells.)\n",
    "\n",
    "  In 'Killer X', an additional rule is that each of the long diagonals\n",
    "  contains each number once.\n",
    "  '''\n",
    "\n",
    "  Here we solve the problem from the Wikipedia page, also shown here\n",
    "  http://en.wikipedia.org/wiki/File:Killersudoku_color.svg\n",
    "\n",
    "  The output is:\n",
    "    2 1 5 6 4 7 3 9 8\n",
    "    3 6 8 9 5 2 1 7 4\n",
    "    7 9 4 3 8 1 6 5 2\n",
    "    5 8 6 2 7 4 9 3 1\n",
    "    1 4 2 5 9 3 8 6 7\n",
    "    9 7 3 8 1 6 4 2 5\n",
    "    8 2 1 7 3 9 5 4 6\n",
    "    6 5 9 4 2 8 7 1 3\n",
    "    4 3 7 1 6 5 2 8 9\n",
    "\n",
    "\n",
    "  Compare with the following models:\n",
    "  * Comet   : http://www.hakank.org/comet/killer_sudoku.co\n",
    "  * MiniZinc: http://www.hakank.org/minizinc/killer_sudoku.mzn\n",
    "  * SICStus: http://www.hakank.org/sicstus/killer_sudoku.pl\n",
    "  * ECLiPSE: http://www.hakank.org/eclipse/killer_sudoku.ecl\n",
    "  * Gecode: http://www.hakank.org/gecode/killer_sudoku.cpp\n",
    "\n",
    "\n",
    "  This model was created by Hakan Kjellerstrand (hakank@gmail.com)\n",
    "  Also see my other Google CP Solver models:\n",
    "  http://www.hakank.org/google_or_tools/\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "code",
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "from ortools.constraint_solver import pywrapcp\n",
    "\n",
    "#\n",
    "# Ensure that the sum of the segments\n",
    "# in cc == res\n",
    "#\n",
    "\n",
    "\n",
    "def calc(cc, x, res):\n",
    "\n",
    "  solver = list(x.values())[0].solver()\n",
    "\n",
    "  # sum the numbers\n",
    "  cage = [x[i[0] - 1, i[1] - 1] for i in cc]\n",
    "  solver.Add(solver.Sum(cage) == res)\n",
    "  solver.Add(solver.AllDifferent(cage))\n",
    "\n",
    "\n",
    "def main():\n",
    "\n",
    "  # Create the solver.\n",
    "  solver = pywrapcp.Solver(\"Killer Sudoku\")\n",
    "\n",
    "  #\n",
    "  # data\n",
    "  #\n",
    "\n",
    "  # size of matrix\n",
    "  n = 9\n",
    "\n",
    "  # For a better view of the problem, see\n",
    "  #  http://en.wikipedia.org/wiki/File:Killersudoku_color.svg\n",
    "\n",
    "  # hints\n",
    "  #    [sum, [segments]]\n",
    "  # Note: 1-based\n",
    "  problem = [[3, [[1, 1], [1, 2]]], [15, [[1, 3], [1, 4], [1, 5]]],\n",
    "             [22, [[1, 6], [2, 5], [2, 6], [3, 5]]], [4, [[1, 7], [2, 7]]],\n",
    "             [16, [[1, 8], [2, 8]]], [15, [[1, 9], [2, 9], [3, 9], [4, 9]]],\n",
    "             [25, [[2, 1], [2, 2], [3, 1], [3, 2]]], [17, [[2, 3], [2, 4]]],\n",
    "             [9, [[3, 3], [3, 4], [4, 4]]], [8, [[3, 6], [4, 6], [5, 6]]],\n",
    "             [20, [[3, 7], [3, 8], [4, 7]]], [6, [[4, 1], [5, 1]]],\n",
    "             [14, [[4, 2], [4, 3]]], [17, [[4, 5], [5, 5], [6, 5]]],\n",
    "             [17, [[4, 8], [5, 7], [5, 8]]], [13, [[5, 2], [5, 3], [6, 2]]],\n",
    "             [20, [[5, 4], [6, 4], [7, 4]]], [12, [[5, 9], [6, 9]]],\n",
    "             [27, [[6, 1], [7, 1], [8, 1], [9, 1]]],\n",
    "             [6, [[6, 3], [7, 2], [7, 3]]], [20, [[6, 6], [7, 6], [7, 7]]],\n",
    "             [6, [[6, 7], [6, 8]]], [10, [[7, 5], [8, 4], [8, 5], [9, 4]]],\n",
    "             [14, [[7, 8], [7, 9], [8, 8], [8, 9]]], [8, [[8, 2], [9, 2]]],\n",
    "             [16, [[8, 3], [9, 3]]], [15, [[8, 6], [8, 7]]],\n",
    "             [13, [[9, 5], [9, 6], [9, 7]]], [17, [[9, 8], [9, 9]]]]\n",
    "\n",
    "  #\n",
    "  # variables\n",
    "  #\n",
    "\n",
    "  # the set\n",
    "  x = {}\n",
    "  for i in range(n):\n",
    "    for j in range(n):\n",
    "      x[i, j] = solver.IntVar(1, n, \"x[%i,%i]\" % (i, j))\n",
    "\n",
    "  x_flat = [x[i, j] for i in range(n) for j in range(n)]\n",
    "\n",
    "  #\n",
    "  # constraints\n",
    "  #\n",
    "\n",
    "  # all rows and columns must be unique\n",
    "  for i in range(n):\n",
    "    row = [x[i, j] for j in range(n)]\n",
    "    solver.Add(solver.AllDifferent(row))\n",
    "\n",
    "    col = [x[j, i] for j in range(n)]\n",
    "    solver.Add(solver.AllDifferent(col))\n",
    "\n",
    "  # cells\n",
    "  for i in range(2):\n",
    "    for j in range(2):\n",
    "      cell = [\n",
    "          x[r, c]\n",
    "          for r in range(i * 3, i * 3 + 3)\n",
    "          for c in range(j * 3, j * 3 + 3)\n",
    "      ]\n",
    "      solver.Add(solver.AllDifferent(cell))\n",
    "\n",
    "  # calculate the segments\n",
    "  for (res, segment) in problem:\n",
    "    calc(segment, x, res)\n",
    "\n",
    "  #\n",
    "  # search and solution\n",
    "  #\n",
    "  db = solver.Phase(x_flat, solver.INT_VAR_DEFAULT, solver.INT_VALUE_DEFAULT)\n",
    "\n",
    "  solver.NewSearch(db)\n",
    "\n",
    "  num_solutions = 0\n",
    "  while solver.NextSolution():\n",
    "    for i in range(n):\n",
    "      for j in range(n):\n",
    "        print(x[i, j].Value(), end=\" \")\n",
    "      print()\n",
    "\n",
    "    print()\n",
    "    num_solutions += 1\n",
    "\n",
    "  solver.EndSearch()\n",
    "\n",
    "  print()\n",
    "  print(\"num_solutions:\", num_solutions)\n",
    "  print(\"failures:\", solver.Failures())\n",
    "  print(\"branches:\", solver.Branches())\n",
    "  print(\"WallTime:\", solver.WallTime())\n",
    "\n",
    "\n",
    "main()\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
